//
//  GoodsOrderDetailsController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/9/3.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "GoodsOrderDetailsController.h"
#import "GoodsOrderDetailsFirstCell.h"

#import "GoodsOrderDetailsSecondCell.h"
#import "GoodsOrderDetailsThirdCell.h"
#import "ConfirmView.h"
#import "OrderPaymentController.h"


@interface GoodsOrderDetailsController ()<UITableViewDelegate,UITableViewDataSource,ConfirmViewOrderDelagate>
{
    MBProgressHUD *progressHUD;
    ConfirmView *lastView;
}

@property(nonatomic,strong)UITableView *orderDetailsView;


@end

@implementation GoodsOrderDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self bulidView];
    
}

- (void)bulidView
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = BACKGROUND_COLOR;
    self.title = @"订单详情";
    
    _orderDetailsView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH, HEIGHT-HEIGHT/9-55) style:UITableViewStylePlain];
    [self.view addSubview:_orderDetailsView];
    _orderDetailsView.backgroundColor = BACKGROUND_COLOR;
    _orderDetailsView.delegate = self;
    _orderDetailsView.dataSource = self;
    _orderDetailsView.showsVerticalScrollIndicator = NO;
    
}

- (void)setStutas_zt:(NSString *)stutas_zt
{
    _stutas_zt  = stutas_zt;
    
    if ([_stutas_zt isEqualToString:@"0"]) {
        lastView = [[ConfirmView alloc] initWithFrame:CGRectMake(0,HEIGHT * 0.8,WIDTH,HEIGHT/9)];
        lastView.delegate = self;
        [self.view addSubview:lastView];
    }
}

#pragma mark 确认下单
- (void)confirmViewOrderDidClick:(UIButton *)button
{
    OrderPaymentController *OrderPaymentVC = [[OrderPaymentController alloc] init];
    OrderPaymentVC.OrderTitleString = @"商品订单";
    OrderPaymentVC.OrderDescribeInfo = @"商品订单";
    OrderPaymentVC.orderType = @"3";
    
    NSArray *listArr = _goodsOrderModel.goods_list;
    GoodsListsModel *goodsListsModel = listArr[0];\
    NSLog(@"%@",goodsListsModel.limit_buy);
    if ([goodsListsModel.limit_buy isEqualToString:@"1"]) {
        OrderPaymentVC.judgeString = goodsListsModel.limit_buy;
    }
    
    OrderPaymentVC.OrderString = _goodsOrderModel.dxh;
    OrderPaymentVC.OrderMonryString = _goodsOrderModel.zongjia;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:OrderPaymentVC];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSArray *listArr = _goodsOrderModel.goods_list;
    return listArr.count + 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray *listArr = _goodsOrderModel.goods_list;
    
    
    if (indexPath.section == 0) {
        
        static NSString *identifier= @"firstCellOrder";
        GoodsOrderDetailsFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            
            cell = [[GoodsOrderDetailsFirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        lastView.totalPrice.text = [NSString stringWithFormat:@"总计:%@元",_goodsOrderModel.zongjia];
        lastView.totalPrice.font = FONT(15);
        lastView.totalPrice.font = [UIFont boldSystemFontOfSize:15];
        
        cell.NameLabel.text = [NSString stringWithFormat:@"收货人: %@   %@",_goodsOrderModel.shouhuoren,_goodsOrderModel.sshouji];
        cell.AddressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",_goodsOrderModel.s_province,_goodsOrderModel.s_city,_goodsOrderModel.s_county,_goodsOrderModel.dizhi];
        return cell;
        
    }else if(indexPath.section == (1+listArr.count)){
        
        GoodsOrderDetailsThirdCell *cell = [[GoodsOrderDetailsThirdCell alloc] init];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.orderIdLabel.text = [NSString stringWithFormat:@"订单编号: %@",_goodsOrderModel.dxh];
        cell.timeLabel.text = [NSString stringWithFormat:@"提交时间: %@",_goodsOrderModel.addtime];
        
        return cell;
        
    }else{
        
        static NSString *identifier= @"ListsCellOrder";
        GoodsOrderDetailsSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            
            cell = [[GoodsOrderDetailsSecondCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        GoodsListsModel *goodsListsModel = listArr[indexPath.section-1];
        cell.goodsImg.image = [UIImage imageNamed:@"goodsLoadJGG.jpg"];
        //图片
        //图片
        [cell.goodsImg sd_setImageWithURL:[NSURL URLWithString:goodsListsModel.picurl] placeholderImage:[UIImage imageNamed:@"goodsLoadJGG"]];
        //        cell.goodsImg.layer.borderWidth = 0.5;
        //        cell.goodsImg.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        
        cell.goodsNameLabel.text = goodsListsModel.title;
        cell.guigeLabel.text = [self getGoodsOrderStandardStrIndexPath:indexPath];
        
        cell.freeLabel.text = [goodsListsModel.jzh integerValue] > 0 ? [NSString stringWithFormat:@"%@元",goodsListsModel.jzh] :  @"包邮";
        cell.priceLabel.text = [NSString stringWithFormat:@"¥%@",goodsListsModel.jiage];
        cell.numLabel.text = [NSString stringWithFormat:@"¥%@",goodsListsModel.nums];
        cell.totalLabel.text = [NSString stringWithFormat:@"¥%@",goodsListsModel.zongjia];
        
        return cell;
        
    }
    
}

#pragma mark 返回商品规格字符串
- (NSString *)getGoodsOrderStandardStrIndexPath:(NSIndexPath *)indexPath
{
    NSArray *goodlistArr = _goodsOrderModel.goods_list;
    GoodsListsModel *goodsListsModel = goodlistArr[indexPath.row];
    
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSString *tempStr = nil;
    if (![goodsListsModel.guige isEqualToString:@"(null)"]) {
        tempStr = [NSString stringWithFormat:@"规格:%@ ",goodsListsModel.guige];
        [arr addObject:tempStr];
    }
    if (![goodsListsModel.xinghao isEqualToString:@"(null)"]) {
        tempStr = [NSString stringWithFormat:@"型号:%@ ",goodsListsModel.xinghao];
        [arr addObject:tempStr];
    }
    if (![goodsListsModel.yscontent isEqualToString:@"(null)"]) {
        tempStr = [NSString stringWithFormat:@"%@ ",goodsListsModel.yscontent];
        [arr addObject:tempStr];
    }
    if (![goodsListsModel.feilv isEqualToString:@"(null)"]) {
        tempStr = [NSString stringWithFormat:@"费率:%@ ",goodsListsModel.feilv];
        [arr addObject:tempStr];
    }
    if (![goodsListsModel.kouwei isEqualToString:@"(null)"]) {
        tempStr = [NSString stringWithFormat:@"口味:%@ ",goodsListsModel.kouwei];
        [arr addObject:tempStr];
    }
    if (![goodsListsModel.chima isEqualToString:@"(null)"]) {
        tempStr = [NSString stringWithFormat:@"尺码:%@ ",goodsListsModel.chima];
        [arr addObject:tempStr];
    }
    if (![goodsListsModel.yanse isEqualToString:@"(null)"]) {
        tempStr = [NSString stringWithFormat:@"颜色:%@ ",goodsListsModel.yanse];
        [arr addObject:tempStr];
    }
    if (![goodsListsModel.taocan isEqualToString:@"(null)"]) {
        tempStr = [NSString stringWithFormat:@"套餐:%@ ",goodsListsModel.taocan];
        [arr addObject:tempStr];
    }
    
    
    NSMutableString * string = [[NSMutableString alloc] init];
    for (NSString *str in arr) {
        [string appendString:str];
    }
    
    return string;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray *listArr = _goodsOrderModel.goods_list;
    if (indexPath.section == 0) {
        return 60;
    }else if (indexPath.section == (1+listArr.count)){
        return 55;
    }else{
        return 175;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


@end
